/**
 * File: SequenceStatisticsProcessor.java
 * Created by: mhaimel
 * Created on: 6 Apr 2009
 * CVS:  $Id: SequenceStatisticsProcessor.java,v 1.3 2009/10/09 14:37:52 mhaimel Exp $
 */
package uk.ac.ebi.velvet.processor.impl;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uk.ac.ebi.curtain.utils.data.FileType;
import uk.ac.ebi.curtain.utils.data.ReadType;
import uk.ac.ebi.curtain.utils.file.EntryMapper;
import uk.ac.ebi.curtain.utils.file.EntryMapperBean;
import uk.ac.ebi.curtain.utils.file.FileInfo;
import uk.ac.ebi.curtain.utils.file.ParseUtil;
import uk.ac.ebi.velvet.processor.MinCutoffStrategy;
import uk.ac.ebi.velvet.processor.StatisticsInfo;
import uk.ac.ebi.velvet.processor.StatisticsProcessor;

/**
 * @author mhaimel
 *
 */
public class SequenceStatisticsProcessor{
	private Log log = LogFactory.getLog(this.getClass());
	private FileInfo seqFile = null;
	private StatisticsProcessor sp = null;
	
	public SequenceStatisticsProcessor(FileInfo statsFile) {
		this.sp = new StatisticsProcessor(statsFile.getFile());
		seqFile = statsFile;
	}
	
	public void addEntry(EntryMapperBean sequ) {
		getStatsProc().addEntry(sequ);
	}

	protected StatisticsProcessor getStatsProc() {
		return sp;
	}

	public StatisticsInfo process() {
		getLog().debug("Process statistics for " + getSeqFile() + " ...");
		gatherStats(getSeqFile());		
		StatisticsInfo stats = processStatistics();
		stats.setExpCov(null);
		stats.setCoverageOfMaxLength(null);
		stats.setCutoffCov(null);
		return stats;
	}

	private void gatherStats(FileInfo seqFile2) {
		ParseUtil seqParser = getSeqFile().getFileType().getParser();
		seqParser.parse(getSeqFile(), new EntryMapper(){
			@Override
			public void mapEntry(EntryMapperBean bean) {
				getStatsProc().addEntry(bean);
			}});
	}

	public StatisticsInfo processStatistics() {
		return getStatsProc().processStatistics();
	}
	

	public StatisticsInfo processStatistics(Long maxtotal) {
		return getStatsProc().processStatistics(maxtotal);
	}

	public void setMaxCov(Integer maxCov) {
		getStatsProc().setMaxCov(maxCov);
	}

	public void setMinCov(Integer minCov) {
		getStatsProc().setMinCov(minCov);
	}

	public void setMinCutoffStrategy(MinCutoffStrategy minCutoffStrategy) {
		getStatsProc().setMinCutoffStrategy(minCutoffStrategy);
	}

	private FileInfo getSeqFile() {
		return seqFile;
	}
	
	private Log getLog() {
		return log;
	}
}
